//
//  DailyCollectionViewCell.m
//  OpenEyes
//
//  Created by apple on 15/8/10.
//  Copyright (c) 2015年 袁阿迅. All rights reserved.
//

#import "DailyCollectionViewCell.h"
#import "UIImageView+WebCache.h"

@interface DailyCollectionViewCell ()

/** 图片*/
@property (nonatomic, strong, readwrite) UIImageView *AXImageView;

@end

@implementation DailyCollectionViewCell

//程序里没用用Nib文件(XIB)时,用代码控制视图内容，需要调用initWithFrame去初始化
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        [self setupImageView];
    }
    return self;
}
//用于视图加载nib文件，从nib中加载对象实例时，使用 initWithCoder初始化这些实例对象
- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self)
    {
        [self setupImageView];
    }
    return self;
}

#pragma mark - Setup Method
- (void)setupImageView
{
    //决定了子视图的显示范围。具体的说，就是当取值为YES时，剪裁超出父视图范围的子视图部分；当取值为NO时，不剪裁子视图。默认值为NO。
    self.clipsToBounds = YES;
    
    //设置图片的大小
    self.AXImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.bounds.origin.x, self.bounds.origin.y, self.bounds.size.width, IMAGE_HEIGHT)];
    //UIViewContentModeScaleAspectFill也会证图片比例不变，但是是填充整个ImageView的，可能只有部分图片显示出来。
    self.AXImageView.contentMode = UIViewContentModeScaleAspectFill;
    self.AXImageView.clipsToBounds = NO;
    
    //在图片上添加一个视图
    _view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, IMAGE_HEIGHT)];
    _view.backgroundColor = [UIColor grayColor];
    _view.alpha = 0.2;
    
    //图片上的标题
    _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, self.bounds.size.width, 20)];
    _titleLabel.font = [UIFont fontWithName:@"ArialHebrew-Bold" size:20];
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    _titleLabel.textColor = [UIColor whiteColor];
    
    //图片上的类型和时间
    _categoryLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 130, self.bounds.size.width, 30)];
    _categoryLabel.font = [UIFont fontWithName:@"ArialHebrew-Bold" size:15];
    _categoryLabel.textAlignment = NSTextAlignmentCenter;
    _categoryLabel.textColor = [UIColor whiteColor];
    
    //添加到视图上
    [self addSubview:self.AXImageView];
    
    [self.AXImageView addSubview:_view];
    [self.AXImageView addSubview:_titleLabel];
    [self.AXImageView addSubview:_categoryLabel];
    
}

//获取图片设置的位置(setter方法)
- (void)setImageOffset:(CGPoint)imageOffset
{
    // 获取图片的位置
    _imageOffset = imageOffset;
    // 获取图片的大小
    CGRect frame = self.AXImageView.bounds;
    //设置图片的位置
    CGRect offsetFrame = CGRectOffset(frame, _imageOffset.x, _imageOffset.y);
    //把设置好的位置重新赋值给现在的图片
    self.AXImageView.frame = offsetFrame;
}
- (void)setModel:(DailyModel *)model
{
    _model = model;
    //解析图片数据
    [self.AXImageView sd_setImageWithURL:[NSURL URLWithString:model.coverForDetail]];
    [self setImageOffset:self.imageOffset];

    //赋值
    self.titleLabel.text = model.title;
    NSString *timeStr = [NSString stringWithFormat:@"%.2ld'%ld\" ",model.duration/60,model.duration%60];
    
    //先拼接字符串，再把字符串#符号改变大小
    NSString *string = [NSString stringWithFormat:@"#%@  /  %@",model.category,timeStr];
    
    NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:string];
    // 设置文本的字体以及大小
    [attString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Helvetica-Bold" size:16] range:NSMakeRange(0, 14)];
    
    self.categoryLabel.attributedText = attString;
    
}

@end
